﻿using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.MC
{
    /// <summary>
    /// 连登信息数据访问类
    /// </summary>
    public class ContinueLoginDal
    {
        /// <summary>
        /// 新增连登信息
        /// </summary>
        /// <param name="continueLogin"></param>
        /// <returns></returns>
        public int AddContinueLogin(MC_ContinueLogin continueLogin)
        {
            string sql = "insert into MC_ContinueLogin(UserID,LoginDays,LoginDate,updateTime) values(@UserID,@LoginDays,@LoginDate,@updateTime)";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, continueLogin.UserID);
            tools.AddSqlParameter("LoginDays", System.Data.SqlDbType.Int, continueLogin.LoginDays);
            tools.AddSqlParameter("LoginDate", System.Data.SqlDbType.DateTime, continueLogin.LoginDate);
            tools.AddSqlParameter("updateTime", System.Data.SqlDbType.DateTime, continueLogin.UpdateTime);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 更新连登记录
        /// </summary>
        /// <param name="continueLogin"></param>
        /// <returns></returns>
        public int UpdateContinueLogin(MC_ContinueLogin continueLogin)
        {
            string sql = "update MC_ContinueLogin set LoginDays=@LoginDays,LoginDate=@LoginDate,updateTime=@updateTime where UserID=@UserID";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, continueLogin.UserID);
            tools.AddSqlParameter("LoginDays", System.Data.SqlDbType.Int, continueLogin.LoginDays);
            tools.AddSqlParameter("LoginDate", System.Data.SqlDbType.DateTime, continueLogin.LoginDate);
            tools.AddSqlParameter("updateTime", System.Data.SqlDbType.DateTime, continueLogin.UpdateTime);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取用户指定日期的连登信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public MC_ContinueLogin GetContinueLogin(int userid)
        {
            string sql = "select top 1 * from MC_ContinueLogin where UserID=@UserID order by updateTime desc";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);

            return SQLHelper.GetDataByColumns<MC_ContinueLogin>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }
    }
}
